package br.ufrn.sigtcc.util.android;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.util.Vector;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import br.ufrn.sigtcc.model.entidades.Curso;

import com.google.gson.Gson;

public class ShowCursos extends AbstractCommand{
	private Curso curso;
	@Override
	public void execute(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		try {
			FacadeShowAndroid fachada =  FacadeShowAndroid.getInstance();
			List<Curso> cursos = fachada.listarCursos();
			//request.setAttribute("cursos", cursos);
			//sets the type of response and the charset used
	        //Be careful, in your mobile client, read the response using the same charset
	        response.setContentType("application/json; charset=UTF-8");
	       
	        System.out.println("Executando: " + request.getParameter("comando") );
	       
	        
	        Vector<Curso> vCursos =  new Vector<Curso>();
	        
	        for (Curso c : cursos) {
	        	 curso =  new Curso();
	        	 
	        	 curso.setNome(c.getNome());
	        	 curso.setHabilitacao(c.getHabilitacao());
	        	 curso.setModalidade(c.getModalidade());
	        	 curso.setTurno(c.getTurno());
	        	 curso.setSede(c.getSede());
				vCursos.add(curso);
			}
	        //convert the clients vector to JSON using GSON, very easy!
	        Gson gson = new Gson();
	        String jsonOutput = gson.toJson(vCursos);
	      
	        //print the response
	     
	        PrintWriter out = response.getWriter();
	        out.println(jsonOutput);
	        out.flush();
	        out.close();
	
		}catch(Exception e){
			e.printStackTrace();
			
		}
		
	}

}
